/**
 * 主功能页面，用于设置行业
 * author:mengxn
 * date:2016年8月23日3:18:42
 */
var saveImgPK="";
var deleImgPK="";
var pk_knowledge_category="";
var kmMaintanceAdd={
	onlike:function(){
			$("#like").click(function() {
				layer.config({
		            extend:'skin/layer.css'
		        });
			    layer.open({
					  type: 2,
					  title:"选择关键字",
					  shade: [0],
					  area: ['1000px', '580px'],
					  shift: 2,
					  content: ['html/bdIndustryChart.html'], //iframe的url，no代表不显示滚动条
			   
			   });
		   });
	},
	//知识类别查所有
	initAll:function(){
		common.showLoading();
		var url = "/PCKnowledgeCategoryEntity/getKnowledgeCategoryByCondition";
		map={"level":1,"fw_parent":'',"pk_user": window.localStorage.userid}
		common.interfaceAjaxFunction(url,map,this.showLeftPartTreeSuccess);
	},
	showLeftPartTreeSuccess:function(json){
		//成功时候
		if(json.status == "0"){
			if(json.data.length > 0){
				var jsonData=json.data;
				var obj=$("#addSelect");
				for(var i=0;i<jsonData.length;i++){
					obj.append("<option value='"+jsonData[i].pk_knowledge_category+"' class='app'   >"+jsonData[i].knowledge_category_name+"</option>");
					}
				
				
			}else{
				//加载设置页面
				layer.msg("当前无知识类别数据，请联系管理员");
			}
		}else{
			//给出信息提示
			layer.msg(json.message);
		}
		//转圈提示去除
		common.hideLoading();
	},

	pageSelect:function(json){
		var obj=$("#appendSele");
		var ht="";
		obj.html("<option value=''>--请选择类别--</option>");
		if(json.status == "0"){
			if(json.data.length > 0){
				var jsonData=json.data;
				for(var i=0;i<jsonData.length;i++){
					ht+="<option value='"+jsonData[i].pk_knowledge_category+"' >"+jsonData[i].knowledge_category_name+"</option>";
					}
				obj.append(ht);
			}else{
				// //加载设置页面
				// layer.msg("当前无行业树结构数据，请联系管理员维护");error(json.message);
		}
		//转圈提示去除
		common.hideLoading();
	}
	},
	//绑定事件
	bindEvent:function(){
		// 取消按钮
		$("#cancle-product").unbind().on("click", function () {
			common.loadPageDiv("html/kmMaintance.html");
		});

		// 新增视频地址按钮
		$("#add_studio_url").unbind().on("click", function () {
			var urlHtml='<span class="del_studio_url" style="color: red">删除</span><input type="text"   class="studio_url" maxlength="200"/>';
			$(this).before(urlHtml);
			//删除视频地址按钮
			$(".del_studio_url").unbind().on("click", function () {
				$(this).prev().remove();
				$(this).remove();
			});
		});

		$("#fabuton").unbind().on("click",function(){
			
			var publish_title=$("#publish_title").val();
			var publish_memo=$("#publish_memo").val();
			var publish_key=$("#publish_key").val();
			var pk_knowledge = jQuery("#addSelect").val();
			var pk_category = jQuery("#appendSele").val();
			var studio_url_list=$(".knowledgeStudiourl input");
			var studio_url='';
			if(studio_url_list!=null&&studio_url_list!=''&&studio_url_list!=undefined){
				var urlTemp='';
				for(var i=0;i<studio_url_list.length;i++){
					urlTemp=studio_url_list[i].value;
					if(urlTemp!=null&&urlTemp.trim()!=''){
						studio_url+=urlTemp+',';
					}
				}
				if(studio_url.charAt(studio_url.length-1)==','){
					studio_url=studio_url.substr(0,studio_url.length-1);
				}
			}
			
			if (pk_knowledge !== null || pk_knowledge !== undefined || pk_knowledge !== '') { 
				pk_knowledge_category= pk_knowledge; 
			}
			if (pk_category === '') {
				 
			}else{
				pk_knowledge_category= pk_category;
			}
			
			if(pk_knowledge_category ===''){
		        layer.msg('请选择商品类别！');
		        return;
		    }
			
			if(publish_title === ''){
		        layer.msg('请填写商品名称');
		        return;
		    }
			if(publish_title.length>100){
		        layer.msg('输入长度应为 0_100!');
		        return;
		    }
			if(saveImgPK ===''){
				if(publish_memo ===''){
			        layer.msg('请填写商品简介');
			        return;
			    }
			}
			
			if(publish_memo.length >1000){
		        layer.msg('输入长度应为 0_1000!');
		        return;
		    }
			if(publish_key ===''){
		        layer.msg('请至少选择一个关键字');
		        return;
		    }
			
			var name1=[];
			name1=publish_key.split(",");
			if(name1.length>20){
				 layer.msg('最多选择20个关键字');
		        return;
			}
			map={
					"publish_memo":publish_memo,
					"publish_title":publish_title,
					"publish_key":publish_key,
						"studio_url":studio_url,
					"km_type":pk_knowledge_category,
					"pk_publish_user":window.localStorage.userid,
					"files_new":saveImgPK.substring(0,saveImgPK.length-1),
					"files_del":deleImgPK.substring(0,deleImgPK.length-1)
				}
			var url ="/PCpublish/insertPublishEntity";
			if(publish_title==null){
				
			}
			common.interfaceAjaxFunction(url,map,kmMaintanceAdd.initPage);
			});
	
	},
	initPage:function(json){
		if(json.status == "0"){
			if(json.message=="数据添加成功!"){
				layer.msg("商品发布成功!");
				//清掉首页缓存的内容
				window.sessionStorage.PCfavoritequeryCountfavorite = "";
				window.sessionStorage.PCpublishqueryRecentKnowledges = "";
				window.sessionStorage.PCpublishqueryKnowledgeCount = "";
				saveImgPK="";
				deleImgPK="";
				common.loadPageDiv("html/kmMaintance.html");
			}
		}
		
	},
	delPage:function(json){
		if(json.status == "0"){
			layer.msg(json.message);
		}
		
	},
	initialize:function(){
		//初始化
		saveImgPK="";
		deleImgPK="";
		
		kmMaintanceAdd.onlike()
		kmMaintanceAdd.initAll();
		kmMaintanceAdd.bindEvent();
		var html="";
		var pdf="";
		 $('#file_upload').Huploadify({
	            'uploader' : '/shellkm/file/uploadFile',
	            'height': 25,
	            'whith' :120,
	            'auto'  : true,
	            'fileDataName':'file',
	            'buttonText' : '选择文件...',
	            'fileTypeExts' : '*.doc; *.docx;  *.xls; *.xlsx; *.ppt; *.pptx; *.pdf; *.jpg; *.jpeg; *.tiff; *.bmp; *.gif; *.mp4; *.png',
	            'multi'    : true,
	            'method'   :'post',
	            'debug':false,
	            'simUploadLimit':6,
	            'onUploadSuccess' : function(file,data, response) {
	            
	            	var obj = jQuery.parseJSON(data);
	            	console.log(JSON.stringify(obj))
	            	if(obj.data.file_type=='0'){
	            		 html ="<div class='updaimg-img'><img src='data:image/png;base64,"+obj.data.pic_string+"' alt='' widht='200' height='200'/><div class='upda-closed onimg' imgId='"+obj.data.pk_att+"'></div></div>";
	            		 $("#img").append(html);
	            			//图片预览
	            	}else{
	            		pdf="<div><p class='glyphicon glyphicon-remove onimg' imgId='"+obj.data.pk_att+"'>"+obj.data.file_name+"<span>"+obj.data.file_size+" </span></p></div>";
	            		$("#pdf").append(pdf);
	            	}
	            	//图片预览
					layer.ready(function(){ //为了layer.ext.js加载完毕再执行
					  layer.photos({
					    photos: '.updaimg'
					  });
					});
	            	
	            	$(".onimg").unbind().on("click", function () {
	            		$(this).parent().remove();
	            		var	del_id=$(this).attr("imgId");
	            		deleImgPK+=del_id+",";
	            		var url = "/file/delFile";
	            		map={
	            				"pk_att":del_id
	            		}
	    				common.interfaceAjaxFunction(url,map,kmMaintanceAdd.delPage);
					});
	            	saveImgPK+=obj.data.pk_att+",";
	            },
	            'onUploadError' : function(file, errorCode, errorMsg, errorString) {
	               } 
	        });
		
		 $("#addSelect").change(function(){
				var LTId=this.value;
				var url = "/PCKnowledgeCategoryEntity/getKnowledgeCategoryByCondition";
				map={"level":2,"fw_parent":LTId,"pk_user":window.localStorage.userid}
	    		common.interfaceAjaxFunction(url,map,kmMaintanceAdd.pageSelect);
				});
		 }
		
};